load("//tools/lint:lint.bzl", "add_lint_tests")
load(
    "//tools/skylark:alias.bzl",
    "drake_cc_library_aliases",
)
load(
    "//tools/skylark:drake_cc.bzl",
    "drake_cc_binary",
    "drake_cc_googletest",
    "drake_cc_googletest_linux_only",
    "drake_cc_library",
    "drake_cc_package_library",
)
load("//tools/skylark:test_tags.bzl", "vtk_test_tags")

package(default_visibility = ["//visibility:public"])

drake_cc_package_library(
    name = "sensors",
    visibility = ["//visibility:public"],
    deps = [
        ":accelerometer",
        ":beam_model",
        ":beam_model_params",
        ":camera_config",
        ":camera_config_functions",
        ":camera_info",
        ":gyroscope",
        ":image",
        ":image_file_format",
        ":image_io",
        ":image_to_lcm_image_array_t",
        ":image_writer",
        ":lcm_image_array_to_images",
        ":lcm_image_traits",
        ":rgbd_sensor",
        ":rgbd_sensor_async",
        ":rotary_encoders",
        ":sim_rgbd_sensor",
    ],
)

drake_cc_library(
    name = "accelerometer",
    srcs = ["accelerometer.cc"],
    hdrs = ["accelerometer.h"],
    deps = [
        "//math:geometric_transform",
        "//multibody/math",
        "//multibody/plant",
        "//multibody/tree:multibody_tree_indexes",
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "beam_model_params",
    srcs = [
        "beam_model_params.cc",
    ],
    hdrs = [
        "beam_model_params.h",
    ],
    deps = [
        "//common:dummy_value",
        "//common:essential",
        "//common:name_value",
        "//common/symbolic:expression",
        "//systems/framework:vector",
    ],
)

drake_cc_library(
    name = "beam_model",
    srcs = ["beam_model.cc"],
    hdrs = ["beam_model.h"],
    deps = [
        ":beam_model_params",
        "//common:unused",
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "camera_config",
    srcs = ["camera_config.cc"],
    hdrs = ["camera_config.h"],
    deps = [
        ":camera_info",
        "//common:name_value",
        "//common/schema:transform",
        "//geometry:rgba",
        "//geometry/render:render_camera",
        "//geometry/render_gl:render_engine_gl_params",
        "//geometry/render_gltf_client:render_engine_gltf_client_params",
        "//geometry/render_vtk:render_engine_vtk_params",
    ],
)

drake_cc_library(
    name = "camera_config_functions",
    srcs = ["camera_config_functions.cc"],
    hdrs = ["camera_config_functions.h"],
    deps = [
        ":camera_config",
        "//geometry:scene_graph",
        "//lcm:interface",
        "//multibody/plant",
        "//systems/framework:diagram_builder",
        "//systems/lcm:lcm_buses",
    ],
    implementation_deps = [
        ":camera_info",
        ":rgbd_sensor",
        ":rgbd_sensor_async",
        ":sim_rgbd_sensor",
        "//common:overloaded",
        "//common/yaml:yaml_io",
        "//geometry/render_gl",
        "//geometry/render_gltf_client",
        "//geometry/render_vtk",
        "//math:geometric_transform",
        "//multibody/parsing:scoped_names",
        "//systems/lcm:lcm_config_functions",
    ],
)

drake_cc_library(
    name = "camera_info",
    srcs = [
        "camera_info.cc",
    ],
    hdrs = [
        "camera_info.h",
    ],
    deps = [
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "gyroscope",
    srcs = ["gyroscope.cc"],
    hdrs = ["gyroscope.h"],
    deps = [
        "//math:geometric_transform",
        "//multibody/math",
        "//multibody/plant",
        "//multibody/tree:multibody_tree_indexes",
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "image",
    srcs = [
        "image.cc",
        "pixel_types.cc",
    ],
    hdrs = [
        "image.h",
        "pixel_types.h",
    ],
    deps = [
        "//common:essential",
        "//common:reset_after_move",
        "//common/symbolic:expression",
    ],
)

drake_cc_library(
    name = "image_file_format",
    srcs = ["image_file_format.cc"],
    hdrs = ["image_file_format.h"],
    deps = [
        "//common:essential",
    ],
)

drake_cc_library(
    name = "image_io",
    srcs = [
        "image_io_internal.cc",
        "image_io_load.cc",
        "image_io_save.cc",
    ],
    hdrs = [
        "image_io.h",
        "image_io_internal.h",
    ],
    install_hdrs_exclude = [
        "image_io_internal.h",
    ],
    deps = [
        ":image",
        ":image_file_format",
        "//common:diagnostic_policy",
        "//common:essential",
        "//common:name_value",
    ],
    implementation_deps = [
        ":vtk_diagnostic_event_observer",
        ":vtk_image_reader_writer",
        "//common:drake_export",
        "@vtk_internal//:vtkCommonCore",
        "@vtk_internal//:vtkCommonDataModel",
        "@vtk_internal//:vtkIOImage",
    ],
)

drake_cc_library(
    name = "lcm_image_traits",
    srcs = [
        "lcm_image_traits.cc",
    ],
    hdrs = [
        "lcm_image_traits.h",
    ],
    deps = [
        ":image",
        "//lcmtypes:image",
    ],
)

drake_cc_library(
    name = "image_to_lcm_image_array_t",
    srcs = [
        "image_to_lcm_image_array_t.cc",
    ],
    hdrs = [
        "image_to_lcm_image_array_t.h",
    ],
    deps = [
        "//common:essential",
        "//lcmtypes:image_array",
        "//systems/framework",
    ],
    implementation_deps = [
        ":lcm_image_traits",
        "@zlib",
    ],
)

drake_cc_library(
    name = "lcm_image_array_to_images",
    srcs = [
        "lcm_image_array_to_images.cc",
    ],
    hdrs = [
        "lcm_image_array_to_images.h",
    ],
    deps = [
        ":image",
        "//common:essential",
        "//systems/framework:leaf_system",
    ],
    implementation_deps = [
        ":lcm_image_traits",
        ":vtk_image_reader_writer",
        "//lcmtypes:image_array",
        "@vtk_internal//:vtkCommonCore",
        "@vtk_internal//:vtkIOImage",
        "@zlib",
    ],
)

drake_cc_binary(
    name = "lcm_image_array_receive_example",
    srcs = [
        "lcm_image_array_receive_example.cc",
    ],
    deps = [
        ":image_to_lcm_image_array_t",
        ":lcm_image_array_to_images",
        "//common:add_text_logging_gflags",
        "//systems/analysis:simulator",
        "//systems/lcm:lcm_pubsub_system",
        "@gflags",
    ],
)

drake_cc_library(
    name = "rgbd_sensor",
    srcs = [
        "rgbd_sensor.cc",
        "rgbd_sensor_discrete.cc",
    ],
    hdrs = [
        "rgbd_sensor.h",
        "rgbd_sensor_discrete.h",
    ],
    deps = [
        ":camera_info",
        ":image",
        "//common:essential",
        "//geometry:geometry_ids",
        "//geometry:scene_graph",
        "//geometry/render:render_engine",
        "//systems/framework:leaf_system",
        "//systems/primitives:zero_order_hold",
    ],
)

drake_cc_library(
    name = "rgbd_sensor_async",
    srcs = ["rgbd_sensor_async.cc"],
    hdrs = ["rgbd_sensor_async.h"],
    deps = [
        ":camera_info",
        ":image",
        ":rgbd_sensor",
        "//geometry:scene_graph",
        "//systems/framework:diagram_builder",
    ],
)

drake_cc_library(
    name = "rotary_encoders",
    srcs = ["rotary_encoders.cc"],
    hdrs = ["rotary_encoders.h"],
    deps = [
        "//common:unused",
        "//systems/framework",
    ],
)

drake_cc_library(
    name = "image_writer",
    srcs = ["image_writer.cc"],
    hdrs = ["image_writer.h"],
    deps = [
        ":image",
        "//common:essential",
        "//systems/framework:leaf_system",
    ],
    implementation_deps = [
        ":image_io",
    ],
)

drake_cc_library(
    name = "sim_rgbd_sensor",
    srcs = ["sim_rgbd_sensor.cc"],
    hdrs = ["sim_rgbd_sensor.h"],
    deps = [
        ":image_to_lcm_image_array_t",
        ":rgbd_sensor",
        "//lcm",
        "//multibody/plant",
        "//systems/framework:diagram_builder",
        "//systems/lcm:lcm_publisher_system",
    ],
)

drake_cc_library(
    name = "vtk_diagnostic_event_observer",
    srcs = ["vtk_diagnostic_event_observer.cc"],
    hdrs = ["vtk_diagnostic_event_observer.h"],
    internal = True,
    visibility = [
        "//geometry/proximity:__pkg__",
        "//geometry/render_vtk:__pkg__",
    ],
    deps = [
        "//common:diagnostic_policy",
        "//common:drake_export",
        "@vtk_internal//:vtkCommonCore",
    ],
)

drake_cc_library(
    name = "vtk_image_reader_writer",
    srcs = ["vtk_image_reader_writer.cc"],
    hdrs = ["vtk_image_reader_writer.h"],
    internal = True,
    visibility = ["//:__subpackages__"],
    deps = [
        ":image_file_format",
        "//common:unused",
        "@vtk_internal//:vtkCommonCore",
        "@vtk_internal//:vtkIOImage",
    ],
)

# === test/ ===

drake_cc_googletest(
    name = "accelerometer_test",
    data = ["//examples/pendulum:models"],
    deps = [
        ":accelerometer",
        "//common/test_utilities:eigen_matrix_compare",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/framework/test_utilities",
    ],
)

drake_cc_googletest(
    name = "beam_model_test",
    deps = [
        ":beam_model",
        "//common/proto:call_python",
        "//systems/analysis:simulator",
        "//systems/framework/test_utilities",
        "//systems/primitives:constant_vector_source",
        "//systems/primitives:random_source",
        "//systems/primitives:vector_log_sink",
    ],
)

drake_cc_googletest(
    name = "camera_config_test",
    deps = [
        ":camera_config",
        "//common/schema:transform",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
        "//common/yaml:yaml_io",
        "//geometry/render_vtk",
        "@fmt",
    ],
)

drake_cc_googletest(
    name = "camera_config_functions_test",
    allow_network = ["render_gltf_client"],
    display = True,
    tags = vtk_test_tags(),
    deps = [
        ":camera_config_functions",
        ":image_to_lcm_image_array_t",
        ":rgbd_sensor_async",
        ":sim_rgbd_sensor",
        "//common/test_utilities:expect_throws_message",
        "//common/yaml:yaml_io",
        "//geometry/render_gl",
        "//geometry/render_gltf_client",
        "//geometry/render_vtk",
        "//lcm:drake_lcm",
        "//systems/lcm:lcm_publisher_system",
    ],
)

drake_cc_googletest(
    name = "camera_info_test",
    deps = [
        ":camera_info",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
    ],
)

drake_cc_googletest(
    name = "image_file_format_test",
    deps = [
        ":image_file_format",
    ],
)

drake_cc_googletest(
    name = "gyroscope_test",
    data = ["//examples/pendulum:models"],
    deps = [
        ":gyroscope",
        "//common/test_utilities:eigen_matrix_compare",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/framework/test_utilities",
    ],
)

drake_cc_googletest(
    name = "image_test",
    deps = [
        ":image",
    ],
)

drake_cc_library(
    name = "image_io_test_params",
    testonly = True,
    srcs = ["test/image_io_test_params.cc"],
    hdrs = ["test/image_io_test_params.h"],
    visibility = ["//visibility:private"],
    deps = [
        ":image",
        ":image_file_format",
        "@gtest//:without_main",
        "@vtk_internal//:vtkCommonCore",
        "@vtk_internal//:vtkCommonDataModel",
    ],
)

drake_cc_googletest(
    name = "image_io_internal_test",
    data = [
        "test/jpeg_test.jpg",
        "test/png_color_test.png",
        "test/tiff_32f_test.tif",
    ],
    deps = [
        ":image_io",
        "//common:find_resource",
        "@vtk_internal//:vtkCommonCore",
    ],
)

drake_cc_googletest(
    name = "image_io_test",
    data = [
        "test/png_color16_test.png",
        "test/tiff_rgb32f_test.tif",
    ],
    deps = [
        ":image_io",
        ":image_io_test_params",
        "//common:find_resource",
        "//common:temp_directory",
        "//common/test_utilities:expect_throws_message",
        "//systems/sensors/test_utilities:image_compare",
    ],
)

drake_cc_googletest(
    name = "image_writer_test",
    tags = vtk_test_tags(),
    deps = [
        ":image_writer",
        "//common:temp_directory",
        "//common/test_utilities",
        "//systems/sensors/test_utilities:image_compare",
    ],
)

drake_cc_googletest(
    name = "image_writer_free_functions_test",
    deps = [
        ":image_writer",
        "//common:temp_directory",
        "//systems/sensors/test_utilities:image_compare",
    ],
)

drake_cc_googletest(
    name = "pixel_types_test",
    deps = [
        ":image",
    ],
)

drake_cc_googletest(
    name = "rgbd_sensor_test",
    tags = vtk_test_tags(),
    deps = [
        ":rgbd_sensor",
        "//common/test_utilities:eigen_matrix_compare",
        "//geometry/test_utilities:dummy_render_engine",
    ],
)

drake_cc_googletest(
    name = "rgbd_sensor_async_test",
    deps = [
        ":rgbd_sensor_async",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:expect_throws_message",
        "//geometry/test_utilities:dummy_render_engine",
        "//multibody/plant",
        "//systems/analysis:simulator",
    ],
)

drake_cc_googletest(
    name = "rgbd_sensor_discrete_test",
    tags = vtk_test_tags(),
    deps = [
        ":rgbd_sensor",
        "//common/test_utilities:eigen_matrix_compare",
        "//geometry/test_utilities:dummy_render_engine",
    ],
)

drake_cc_googletest_linux_only(
    name = "rgbd_sensor_async_gl_test",
    timeout = "moderate",
    data = [
        ":test/rgbd_sensor_async_gl_test.dmd.yaml",
        "@drake_models//:manipulation_station",
    ],
    display = True,
    # TODO(#21420) This test is currently broken on Ubuntu 24.04 ("Noble").
    enable_condition = "//tools:ubuntu_jammy",
    tags = vtk_test_tags() + [
        # Similar to #7520, the GL vendor's libraries are not sufficiently
        # instrumented for compatibility with TSan.
        "no_tsan",
    ],
    deps = [
        ":image_writer",
        ":rgbd_sensor",
        ":rgbd_sensor_async",
        "//common/trajectories:piecewise_pose",
        "//geometry/render_gl",
        "//multibody/parsing",
        "//multibody/plant",
        "//systems/analysis:simulator",
    ],
)

drake_cc_googletest(
    name = "rotary_encoders_test",
    deps = [
        ":rotary_encoders",
        "//common/test_utilities:eigen_matrix_compare",
        "//common/test_utilities:symbolic_test_util",
        "//systems/framework/test_utilities",
    ],
)

drake_cc_googletest(
    name = "image_to_lcm_image_array_t_test",
    deps = [
        ":image",
        ":image_to_lcm_image_array_t",
    ],
)

drake_cc_googletest(
    name = "lcm_image_array_to_images_test",
    data = glob([
        "test/*.jpg",
        "test/*.png",
    ]),
    deps = [
        ":lcm_image_array_to_images",
        "//common:find_resource",
        "//lcmtypes:image_array",
    ],
)

drake_cc_googletest(
    name = "sim_rgbd_sensor_test",
    deps = [
        ":image_to_lcm_image_array_t",
        ":rgbd_sensor",
        ":sim_rgbd_sensor",
        "//common/test_utilities:eigen_matrix_compare",
        "//lcm:drake_lcm",
        "//multibody/plant",
        "//systems/framework:diagram_builder",
        "//systems/lcm:lcm_publisher_system",
        "@fmt",
    ],
)

drake_cc_googletest(
    name = "vtk_diagnostic_event_observer_test",
    deps = [
        ":vtk_diagnostic_event_observer",
    ],
)

drake_cc_googletest(
    name = "vtk_image_reader_writer_test",
    deps = [
        ":image_io_test_params",
        ":vtk_image_reader_writer",
        "//common:temp_directory",
        "//common/test_utilities:expect_throws_message",
        "@vtk_internal//:vtkCommonCore",
        "@vtk_internal//:vtkCommonDataModel",
        "@vtk_internal//:vtkIOImage",
    ],
)

add_lint_tests()
